package com.gl.doc.operation.comment.entity;

import com.gl.doc.model.Paragraph;
import com.gl.doc.model.SubParagraph;
import com.gl.doc.model.WordComment;
import com.gl.doc.operation.comment.entity.TargetSentenceIndex;
import com.gl.doc.operation.comment.entity.TargetSubParagraphContentIndex;
import com.gl.doc.support.ParagraphLookupSupport;
import com.gl.doc.support.StringSupport;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * 批注位置
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommentLocation {
    /**
     * word文档批注
     */
    private WordComment wordComment;
    /**
     * 需要添加批注的句子或者文字 坐标存储对象集合
     */
    private List<TargetSentenceIndex> targetSentenceIndexList;
    /**
     * 添加批注的子段落：需要添加批注的句子或文字出现在字段落的坐标集合
     */
    private List<TargetSubParagraphContentIndex> targetSubParagraphContentIndexList;

    /**
     * 获取添加批注的句子或者文字 坐标存储对象集合
     *
     * @return 需要添加批注的句子或者文字 坐标存储对象集合
     */
    public List<TargetSentenceIndex> getTargetSentenceIndexList() {
        //先解析这个需要加批注的 文字 在段落里面的位置
        String targetSentence = wordComment.getTargetSentence();
        Paragraph targetParagraph = wordComment.getTargetParagraph();
        String targetParagraphContent = targetParagraph.getContent();
        List<TargetSentenceIndex> targetSentenceIndexList = new ArrayList<>();
        StringSupport.indexOfSupport(targetSentence, targetParagraphContent, (startIndex, endIndex) -> {
            TargetSentenceIndex targetSentenceIndex = new TargetSentenceIndex();
            targetSentenceIndex.setStartIndex(startIndex);
            targetSentenceIndex.setEndIndex(endIndex);
            targetSentenceIndexList.add(targetSentenceIndex);
        });
        this.setTargetSentenceIndexList(targetSentenceIndexList);
        return targetSentenceIndexList;
    }

    /**
     * 获取添加批注的句子或文字出现在字段落的坐标集合
     *
     * @return 添加批注的句子或文字出现在字段落的坐标集合
     */
    public List<TargetSubParagraphContentIndex> getTargetSubParagraphContentIndexList() {
        //先查找最新的段落元素
        Paragraph targetParagraph = wordComment.getTargetParagraph();
        Element targetParagraphElement = targetParagraph.getElement();
        String idPath = targetParagraph.getIdPath();
        targetParagraph = ParagraphLookupSupport.getParagraph(targetParagraphElement, targetParagraphElement.elements());
        targetParagraph.setIdPath(idPath);
        wordComment.setTargetParagraph(targetParagraph);
        //解析段落里面每小段文字的位置
        List<SubParagraph> subParagraphs = targetParagraph.getSubParagraphs();
        List<TargetSubParagraphContentIndex> targetSubParagraphContentIndexList = new ArrayList<>();
        int startIndex = 0;
        for (SubParagraph subParagraph : subParagraphs) {
            TargetSubParagraphContentIndex targetSubParagraphContentIndex = new TargetSubParagraphContentIndex();
            String content = subParagraph.getContent();
            int endIndex = startIndex + content.length();
            targetSubParagraphContentIndex.setStartIndex(startIndex);
            startIndex = endIndex;
            targetSubParagraphContentIndex.setEndIndex(endIndex - 1);
            targetSubParagraphContentIndex.setSubParagraph(subParagraph);
            targetSubParagraphContentIndexList.add(targetSubParagraphContentIndex);
        }
        this.setTargetSubParagraphContentIndexList(targetSubParagraphContentIndexList);
        return targetSubParagraphContentIndexList;
    }
}
